package com.example.loginmvvm.player

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.example.loginmvvm.R
import kotlinx.android.synthetic.main.activity_flow_player.*

/*
 * class title: 
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2021/4/19.
 * PS: Not easy to write code, please indicate.
 */
class FlowPlayerControllerActivity : AppCompatActivity(), IPlayerCallback {

    /**
     * by lazy 懒加载，只执行一次
     */
    private val playerPresenter by lazy {
        //这里就不是同一个对象了
        PlayerPresenter.instance
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flow_player)

        initListener()
        initDataListener()
    }

    private val livePlayerObserver by lazy {
        LivePlayerStateObserver()
    }

    class LivePlayerStateObserver : Observer<PlayerPresenter.PlayState> {
        override fun onChanged(t: PlayerPresenter.PlayState?) {
            println("FlowPlayerControllerActivity播放器界面...live data...>当前的状态$t")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        LivePlayerState.instance.removeObserver(livePlayerObserver)
    }

    private fun initDataListener() {
        LivePlayerState.instance.observeForever(livePlayerObserver)
    }

    private fun initListener() {
        playOrePauseBtn.setOnClickListener {
            playerPresenter.doPlayOrPause()
        }
    }

    override fun onTitleChange(title: String) {
        TODO("Not yet implemented")
    }

    override fun onProgressChange(current: Int) {
        TODO("Not yet implemented")
    }

    override fun onPlaying() {
        playOrePauseBtn.text = "暂停"
    }

    override fun onPlayerPause() {
        playOrePauseBtn.text = "播放"
    }

    override fun onCoverChange(cover: String) {
        TODO("Not yet implemented")
    }
}